<?php

namespace App\Models\IndexConfig;

use App\Models\Products\ProductDescription;
use Illuminate\Console\Scheduling\Schedule;
use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

/**
 * Class DirewolfIndexConfigurator
 * @package App
 *
 * @link https://github.com/babenkoivan/scout-elasticsearch-driver - 配置文档
 * @example
 * 每一个 Model 必须写建立自己的索引配置器
 *     php artisan elastic:create-index App\\Models\\IndexConfig\\ProductDescriptionIndexConfigurator
 * 然后可以使用 scout 的 import
 *      php artisan scout:import "\App\Models\Products\ProductDescription"
 * 清除索引
 *      php artisan scout:flush "\App\Models\Products\ProductDescription"
 */
class ProductDescriptionIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    /**
     * 可不写，默认是类名去掉 Configurator 的 snake 形式
     *
     * @var string
     */
    protected $name = 'direwolf_product_description_index';

    /**
     * @var array
     */
    protected $settings = [
        //
    ];

    /**
     * 定期更新索引
     *
     * @param Schedule $schedule
     */
    public static function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            $project_dir = base_path();
            $class = ProductDescription::class;
            exec("cd $project_dir && php artisan scout:import '$class'");
        })
        ->daily();
    }
}